package com.mysteel.monitor.controller;


import com.mysteel.monitor.core.annotation.Monitor;
import com.mysteel.monitor.core.annotation.MonitorMethod;
import com.mysteel.monitor.core.command.ThreadCommand;
import com.mysteel.monitor.core.response.RespResult;
import com.mysteel.monitor.core.utils.ThreadUtil;

import javax.servlet.http.HttpServletRequest;


/**
 * 获取线程信息
 * @author:admin
 */
@Monitor("/v1/api/thread")
public class ThreadController {

    ThreadCommand threadCommand = new ThreadCommand();
  //thread -n 3 当前最忙的前N个线程并打印堆栈

  // thread 显示所有线程的信息

  // thread id  显示指定线程的运行堆栈
    /**
     *  在线诊断信息
     * 线程信息
     * @return
     */
    @MonitorMethod("detail")
    public RespResult threadDetail(HttpServletRequest request){
        Long threadId = Long.parseLong(request.getParameter("id"));
        // 获取初始话信息
        return RespResult.success(ThreadUtil.getFullStacktrace(ThreadUtil.getThreadById(threadId),-1));
    }
    // thread -b, 找出当前阻塞其他线程的线程

    /**
     *  最繁忙线程
     * @return
     */
    @MonitorMethod("busy")
    public RespResult busyDetail(HttpServletRequest request){
        int topN = Integer.parseInt(request.getParameter("top"));
        // 最繁忙的线程
        return RespResult.success(threadCommand.processTopBusyThreads(topN));
    }


    /**
     *  线程总览
     * @return
     */
    @MonitorMethod("overview")
    public RespResult overViewDetail(){
        // 获取初始话信息
        return RespResult.success(threadCommand.processAllThreads());
    }

    /**
     *  最繁忙线程
     * @return
     */
    @MonitorMethod("block")
    public RespResult blockDetail(){
        // 获取初始话信息
        return RespResult.success(threadCommand.processBlockingThread());
    }

}
